// sentry.txt - a hostile guard that goes out on patrol. when it sees party,
// fights for 2 rounds and then runs back to get help.

// Memory Cells:
//   Cell 0 - Dialogue node when spoken to
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//	 Cell 3 - The nav point the character marches out to. If left at 12, just 
//    waits.

begincreaturescript;

variables;

short i,target;
short gave_warning = 0;
short shouted_alarm = 0;
short fight_count = 0;
short last_abil;
short path_a_or_b = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();
	set_act_at_dist(ME,1);
	break;

beginstate DEAD_STATE;

break;

beginstate START_STATE; 

	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,12,0)) {
		if (dist_to_party() > 8)
			approach_char(ME,30000,7);
			else {
				do_attack();
				set_state(3);
				}
		}
		
	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
		
	// if we've given alarm, hunt party down
	if ((get_attitude(ME) >= 10) && (shouted_alarm > 0)) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}
	
	// goes out on patrol
	if (get_memory_cell(3) < 12) {
		if (path_a_or_b == 0) {
			if (get_ran(1,0,100) < 25) {
				if (approach_nav_point(ME,get_memory_cell(3),2))
					path_a_or_b = 1;	
				}	
			}
			else {
				if (get_ran(1,0,100) < 90) {
					if (return_to_start(ME,2))
						path_a_or_b = 0;		
					}
				}
		}
		
	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_ran(1,0,100) < 80) && (is_combat()) && (get_attitude(ME) >= 10) && (tick_difference(last_abil,get_current_tick()) > 0) && (fight_count < 2) && (shouted_alarm == 0) && (get_memory_cell(3) < 12)) {
		fight_count = fight_count + 1;
		last_abil = get_current_tick();
		if (fight_count == 2) {
			if (shouted_alarm == 0) {
				shouted_alarm = 1;
				print_named_str(ME,"shouts an alarm!");
				run_text_on_char(ME,"Shouts alarm!",0);
				set_attitude(30000 + what_group_in(ME),10);
				set_act_at_dist(30000 + what_group_in(ME),1);
				alert_char(30000 + what_group_in(ME));
				}
			}
		}
		
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate 4; // back to start
		shouted_alarm = 1;
		print_named_str(ME,"shouts an alarm!");
		run_text_on_char(ME,"Shouts alarm!",0);
		set_attitude(30000 + what_group_in(ME),10);
		set_act_at_dist(30000 + what_group_in(ME),1);
		alert_char(30000 + what_group_in(ME));
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		print_str("Talking: It doesn't respond.");
		end();
		}
	begin_talk_mode(get_memory_cell(3));
break;